#!/bin/sh
psid=`ps awwx| grep [dsl]oadbatch | cut -d ' ' -f 1`
for ps in $psid
do
    if [ $ps != $$ ]; then
	echo another downloading process $ps is running. Exting ...
	echo `ps awwx | grep [dsl]oadbatch`
    fi
done

pwd=`pwd`
DS_DIR=/opt/inquiry/ds
. $DS_DIR/env.sh
export BLASTDB=$DB_PATH
export NODECOUNT=$NODES
export PATH=$DS_DIR:$PATH
export SUB_FILE=$DS_DIR/sub.txt


if [ ! -f $DS_DIR/fpatch ]; then
    cd $DS_DIR;
    make -f makepatch
    cd $pwd
fi
if [ ! -f $DS_DIR/fpatch ]; then
    echo can not make fpatch
    exit 2
fi

if [ ! -d $DB_PATH ]; then
    mkdir -p $DB_PATH
fi
if [ ! -d $DB_PATH ]; then
    echo $DB_PATH does not exist
    exit 3
fi
if [ ! -w $DB_PATH ]; then
    echo do not have write permission to $DB_PATH
    exit 4
fi
if [ ! -d $DB_TMP ]; then
    mkdir -p $DB_TMP
fi
if [ ! -d $DB_TMP ]; then
    echo $DB_TMP does not exist
    exit 5
fi
if [ ! -w $DB_TMP ]; then
    echo do not have write permission to $DB_TMP
    exit 6
fi

if [ $DB_PATH = $DB_TMP ]; then
    echo DB_PATH and DB_TMP can not be the same directory
    exit 7
fi

binary=`which btformatdb`
if [ ! -n $binary ]; then
    echo btformatdb is not found
    exit 8
fi
binary=`which formatdb`
if [ ! -n $binary ]; then
    echo formatdb is not found
    exit 9
else
    binary=`which mpiformatdb`
    if [ ! -n $binary ]; then
        echo mpiformatdb is not found
        exit 10
    fi
fi

classpath="."
appwebdir=/opt/inquiry/apache-tomcat-6.0.18/webapps/appweb/WEB-INF
files=`find $appwebdir -name "*.jar"`
for file in $files
do
    classpath=${classpath}:$file
done
#classpath=${classpath}:$appwebdir/modules/rampart-1.4.mar:$appwebdir/modules/rahas-1.4.mar:$appwebdir/classes
classpath=${classpath}:$appwebdir/classes
#echo $classpath
echo run command
rm -f $DS_DIR/ds.log
#echo java -cp $classpath net.bioteam.appweb.ws.client.program.ProgramTest $@
java -Dlog4j.configuration=dslog.properties -cp $classpath net.bioteam.appweb.ds.DSClient $@
